<!-- 我的课程 -->
<template>
	<view class="my-course">
		<view v-if="list.length>0" class="course-list">
			<view v-for="(item, index) in list" :key="index" class="course-item">
				<view class="top-row flex-box">
					<view @click="to('/mainProcess/club/detail/detail?id='+item.clubId)" class="left flex-box">
						<image class="club-icon" src="@/static/images/icon-vipcard-club.png" mode=""></image>
						<view class="club-name">
							{{item.clubName}}
						</view>
						<van-icon name="arrow" size="12px" color="#CCCCCC" />
					</view>
					<view class="right status">
						<!-- {{matchStatus(item)}} -->
						{{item.classStateStr}}
					</view>
				</view>
				<view @click="toDetail(item.classId)" class="course-info flex-box">
					<view class="left">
						<image :src="item.coachIcon||item.classImgUrl" mode=""></image>
					</view>
					<view class="right">
						<view class="title-price flex-box">
							<view class="course-title line-clamp-2">
								{{item.className}}
							</view>
							<view v-if="item.cost!==undefined&&item.cost!==null" class="course-price">
								￥<text>{{item.cost|floorTwo}}</text>
							</view>
						</view>
						<view class="venue">
							{{item.placeName}}
						</view>
						<view class="tags flex-box">
							<view class="lit-tag">
								{{item.classTypeValue}}
							</view>
						</view>
					</view>
					<!-- 产品让去掉列表的约课按钮 -->
					<!-- <view v-if="item.classState==1&&['0','1'].includes(item.classType)&&item.isHave==1&&item.status=='0'" class="btn-appoint">
						约课
					</view> -->
				</view>
			</view>
		</view>
		<xjx-nodata v-else-if="pageLoaded"></xjx-nodata>
		<view v-if="list.length==0&&pageLoaded" class="guide-box flex-box">
			<view @click="toCourseList" class="guide-btn">
				立即选课
			</view>
		</view>
		<view v-if="list.length>0" class="bottom-text">
			<van-loading v-if="!allPagesLoaded" type="spinner" :size="24"/>
			<text v-else>我也是有底线的～</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				list: [],
				pageLoaded: false,
				pageNo: 0,
				defaultPageSize: 20,
				allPagesLoaded: false
			};
		},
		onLoad(data) {
			this.getList()
		},
		computed: {
			...mapState(['userInfo', 'accessToken']),
		},
		// 页面相关事件处理函数--监听用户下拉动作
		async onPullDownRefresh() {
			//调用刷新时将执行的方法
			this.pageNo = 0
			this.allPagesLoaded = false
			await this.getList(true)
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if(!this.pageLoaded||this.allPagesLoaded){
				return false
			}
			this.getList()
		},
		methods: {
			async getList(refresh) {
				this.pageLoaded = false
				const param = {
					'ACCESS-TOKEN': this.accessToken,
					pageSize: this.defaultPageSize,
					pageNo: this.pageNo+1,
					isPay: 1
				}
				const res = await this.$api.queryClassEnterByUser(param)
				if (res && res.code == 200) {
					this.pageLoaded = true
					if(refresh){
						this.list = []
					}
					if(res.data.list){
						this.list.push(...res.data.list)
						this.pageNo++
						if(this.list.length==res.data.count){
							this.allPagesLoaded = true
						}
					}
				}
			},
			matchStatus(item) {
				if (item.classStatus == '1') {
					return item.classStatusValue
				} else if (item.orderStatus == '4' || (item.orderStatus == '6' && item.isRefund == '1')) {
					return item.orderStatusName
				} else {
					return item.classStateStr
				}
			},
			to(url) {
				uni.navigateTo({
					url: url
				})
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/personalCenter/myCourse/detail/detail?id=${id}`
				})
			},
			toAppoint(id, classid) {
				uni.navigateTo({
					url: `/personalCenter/myCourse/appoint/appoint?id=${id}&classId=${classid}`
				})
			},
			toCourseList() {
				uni.switchTab({
					url: '/pages/course/index/index'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.my-course {
		min-height: 100vh;
		overflow: hidden;
		padding-bottom: 15rpx;
		background-color: #FFF;
		.bottom-text{
			text-align: center;
			line-height: 40px;
			height: 80rpx;
			font-size: 24rpx;
			color: #999;
		}
		.course-item {
			// min-height: 280rpx;
			box-sizing: border-box;
			padding: 25rpx 30rpx;

			.top-row {
				height: 30rpx;

				.left {
					align-items: center;

					.club-icon {
						width: 32rpx;
						height: 32rpx;
					}

					.club-name {
						font-size: 26rpx;
						line-height: 30rpx;
						margin: 0 10rpx 0 15rpx;
						color: #999999;
					}
				}

				.right {
					color: #FDB42E;
					font-size: 26rpx;
					line-height: 30rpx;
				}
			}

			.course-info {
				margin-top: 18rpx;
				align-items: stretch;
				position: relative;

				.left {
					width: 126rpx;
					height: 126rpx;
					margin-right: 20rpx;
					flex: 0 0 auto;
					background-color: #F2F2F2;
					border-radius: 50%;

					image {
						width: 100%;
						height: 100%;
						vertical-align: middle;
						border-radius: 50%;
					}
				}

				.right {
					flex: 1 0 auto;
					display: flex;
					flex-flow: column;
					justify-content: flex-start;

					.title-price {
						align-items: flex-start;

						.course-title {
							line-height: 38rpx;
							max-width: 360rpx;
						}

						.course-price {
							font-size: 24rpx;
							line-height: 38rpx;

							text {
								font-size: 30rpx;
							}
						}
					}

					.venue {
						font-size: 24rpx;
						color: $text-color-grey;
						text-overflow: ellipsis;
						white-space: nowrap;
						overflow: hidden;
						margin-top: 6rpx;
						max-width: 540rpx;
					}

					.tags {
						justify-content: flex-start;
						margin-top: 6rpx;
					}
				}

				.btn-appoint {
					position: absolute;
					right: 0;
					bottom: 0;
					width: 125rpx;
					height: 43rpx;
					background: #6BA9FE;
					border-radius: 6rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					line-height: 42rpx;
					text-align: center;
				}
			}
		}

		.guide-box {
			justify-content: center;

			.guide-btn {
				height: 78rpx;
				line-height: 74rpx;
				border: 2rpx solid #2E87FF;
				border-radius: 39rpx;
				color: #2E87FF;
				padding: 0 45rpx;
				display: inline-block;
				margin: 20rpx auto;
			}
		}
	}
</style>
